package com.xiaohe.service;

import com.xiaohe.dao.UserDao;
import com.xiaohe.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 功能描述
 */
@Service
@Transactional//事务
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private UserDao userDao;
    /**
     * 用户注册
     * */
    @Override
    public void register(User user) {

        //1生成用户状态
//        user.setStatus("已激活");
//        user.setRegisterTime(new Date());
        userDao.save(user);
    }

    /**
     * 利用用户名查询用户是否存在
     * @param userUsername
     * @return
     */
    @Override
    public User selectUserName(String userUsername) {
        return userDao.selectUserName(userUsername);
    }

    /**
     * 根据用户名查询用户是否存在
     * */
    @Override
    public User findUserByName(String userUsername,String userPassword) {
        return userDao.findUserByName(userUsername,userPassword);
    }

    /**
     * 用户登录
     * */
    @Override
    public User login(User user) {

        //根据用户名进行查询
        User userDB = userDao.findUserByName(user.getUserUsername(), user.getUserPassword());
        if(userDB!=null){
            if(userDB!=null){
                System.out.println("登录成功！");
                //相同,登录成功
                  return user;
            }else {
                System.out.println("登录失败！");
                throw new RuntimeException("密码输入不正确");
            }

        }else {
            System.out.println("该用户不存在！");
            throw new RuntimeException("该用户不存在");
        }

    }
}